﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.Model;
using AiXiu.IDAL;
using AiXiu.IBLL;
using AiXiu.DAL;
using AiXiu.Common;
using AiXiu.Common.Vod;
using AiXiu.Model.Vod;

namespace AiXiu.BLL
{
    public class VideoBLL : IVideoBLL
    {
        private IVideoDAL videoDAL = new VideoDAL();
        private VodHelper vodHelper = new VodHelper();

        public CreateUploadVideoResult uploadVideo(string title, string fileName, string location, int userId)
        {
            CreateUploadVideoResult vodret = vodHelper.CreateUploadVideo(title, fileName);

            TBVideos videoInfo = new TBVideos{
                UserId = userId,
                Headline = title,
                Location = location,
                VideoId = vodret.VideoId,
                Status = Convert.ToInt32(VideoStatus.Uploading)
            };

            videoDAL.addVideo(videoInfo);
            return vodret;
        }

        public int UpdateVideoState()
        {
            //获取需要更新的视频id集合
            List<string> videoIds = videoDAL.GetVideosIdByState();

            //如果有需要更新的视频数据
            if (videoIds.Count > 0)
            {
                #region 一次最多获取20个视频信息
                int startIndex = 0;         //记录每次获取20个视频的开始下标
                int retCount = 0;           //记录更新的视频数
                while (startIndex < videoIds.Count)
                {
                    //需要取的视频数量
                    int getCount = Math.Min(startIndex + 20, videoIds.Count) - startIndex;
                    GetVideoInfosResult result = vodHelper.GetVideoInfos(videoIds.Skip(startIndex).Take(getCount));

                    retCount += videoDAL.UpdateVideosState(result.VideoList);
                    startIndex += getCount;
                }
                #endregion
            }
            return 0;
        }

        public List<TBVideos> GetVideosList()
        {
            return videoDAL.GetVideosList();
        }

        public TBVideos GetVideoById(string videoId)
        {
            return videoDAL.GetVideoById(videoId);
        }

        public GetPlayInfoResult InitPlay(string videoId)
        {
            return new VodHelper().GetPlayInfo(videoId);
        }

        public List<MyVideoModel> GetTBVideosByUserId(int userId)
        {
            List<TBVideos> videos = videoDAL.GetTBVideosByUserId(userId);
            var myVideos = new List<MyVideoModel>();
            foreach (TBVideos item in videos)
            {
                myVideos.Add(new MyVideoModel(item));
            }
            return myVideos;
        }

        public OperResult DeleteVideo(string videoId)
        {
            vodHelper.DeleteVideos(new List<string> { videoId });
            if (videoDAL.DeleteVideoById(videoId) > 0)
            {
                return OperResult.Succeed();
            }
            else
            {
                return OperResult.Failed();
            }
        }
    }
}
